package com.swak.storage;

import java.io.File;

import com.swak.fx.support.ui.UI.Conts;
import com.swak.serializer.KryoSerializer;
import com.swak.serializer.Serializer;

/**
 * 存储目录
 * 
 * @author 618lf
 */
public class LocalStorage extends AbstractStorage {

	private static LocalStorage ME = new LocalStorage();
	private final Serializer serializer;

	public static LocalStorage New() {
		return ME;
	}

	private final File storagePath;

	private LocalStorage() {
		this.storagePath = new File(tempPath, Conts.StoragePath);
		if (!this.storagePath.exists()) {
			this.storagePath.mkdirs();
		}
		this.serializer = new KryoSerializer();
	}

	@Override
	protected File getStoragePath() {
		return storagePath;
	}

	public static void Put(String name, Object obj) {
		byte[] datas = ME.serializer.serialize(obj);
		ME.put(name, datas);
	}

	public static <T> T Get(String name) {
		byte[] datas = ME.get(name);
		return ME.serializer.deserialize(datas);
	}
}
